package data.dblp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;


public class GeneratorData {

	/**
	 * @param args
	 */
	public static int total=2400000;
	public static String folder="/home/quyet/test/dblp/dataset/2400000";
	
	public static void main(String[] args) {
		GetData(folder+"/part1-16.txt");
	}
	
	public static void GetData(String filepath) {
		// TODO Auto-generated method stub

		try {
			FileReader fr = new FileReader(new File(filepath));
			BufferedReader br = new BufferedReader(fr);

			FileWriter fEdge = new FileWriter(folder + "/edges.txt");
			FileWriter fLabel = new FileWriter(folder + "/labels.txt");
			FileWriter fInfo = new FileWriter(folder + "/info.txt");
			BufferedWriter bwEdge = new BufferedWriter(fEdge);
			BufferedWriter bwLalel = new BufferedWriter(fLabel);
			BufferedWriter bwInfo = new BufferedWriter(fInfo);

			HashSet<String> listL = new HashSet<String>();

			String strLine;
			HashMap<String, Node> V = new HashMap<String, Node>();			
			int countEdges = 0;
			int line = 0;

			while ((strLine = br.readLine()) != null) {

				String[] row = strLine.split("\t");
				if (row.length >= 6) {

					 if (!listL.contains(row[4])) {
					 listL.add(row[4]);
					 bwLalel.write(row[4] + "\n");
					 }

					Node ni = new Node(Integer.parseInt(row[0]));					

					for (int i = 6; i < row.length; i++) {
						Node m = V.get(row[i]);
						if (m == null) {
							V.put(row[i], new Node(Integer.parseInt(row[i])));
							bwEdge.write(ni.id + "\t" + row[i]+ "\n");							
						} else {
							bwEdge.write(ni.id + "\t" + m.id + "\n");							
						}
						countEdges += 1;
					}					
				}
				line += 1;			
			
				System.out.println("Running " + (V.size()+countEdges) + "/" + total + " -> "
						+ (100 * (V.size()+countEdges) / total) + "% -> line: "+line);
				if(V.size()+countEdges>=total)
					break;
			}

			bwInfo.write(V.size() + "\t" + countEdges + "\t"
					+ (V.size() + countEdges) +"\n");
			bwInfo.write("number of lables: "+listL.size() +"\n");
			bwInfo.write("line: "+line+" in file "+filepath);
			br.close();
			bwInfo.close();
			bwLalel.close();			
			bwEdge.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}

class Node {
	int id;
	String label;
	ArrayList<Integer> neighbors;

	public Node() {		
		this.label = "";
	}

	public Node(int id) {
		this.id = id;
	}

	public Node(Integer id, String label) {
		this.id = id;
		this.label = label;
	}

	public Node(Integer id, ArrayList<Integer> neighbors) {
		this.id = id;
		this.neighbors = neighbors;
	}

	public Node(Integer id, String label, ArrayList<Integer> neighbors) {
		this.id = id;
		this.label = label;
		this.neighbors = neighbors;
	}

	@Override
	public boolean equals(Object obj) {
		boolean check = false;
		if (obj == null)
			return false;
		else if (obj == this)
			return true;
		else {
			if (obj instanceof Node && obj.getClass() == this.getClass()) {
				final Node t = (Node) obj;
				check = (this.id == t.id);
			}
		}
		return check;
	}

	@Override
	public int hashCode() {
		int hash = 7;
		hash = (37 * hash) + this.id;
		return hash;
	}
}